#include "DraggableLineItem.h"
#include <QCursor>
#include <QGraphicsScene>

DraggableLineItem::DraggableLineItem(QGraphicsItem *parent) :
    QObject(nullptr),
    QGraphicsLineItem(parent),
    m_isVertical(false),
    m_isDragging(false)
{
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    setAcceptHoverEvents(true);
}

DraggableLineItem::DraggableLineItem(const QLineF &line, QGraphicsItem *parent) :
    QObject(nullptr),
    QGraphicsLineItem(line, parent),
    m_isVertical(false),
    m_isDragging(false)
{
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    setAcceptHoverEvents(true);
    
    // 根据线条斜率判断是否为垂直线
    if (qFuzzyCompare(line.dx(), 0.0)) {
        m_isVertical = true;
    }
}

void DraggableLineItem::setVertical(bool isVertical)
{
    m_isVertical = isVertical;
}

bool DraggableLineItem::isVertical() const
{
    return m_isVertical;
}

void DraggableLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isDragging = true;
        m_dragOffset = event->pos();
        QGraphicsLineItem::mousePressEvent(event);
        event->accept();
    }
}

void DraggableLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (m_isDragging && scene()) {
        // 获取场景中的鼠标位置
        QPointF scenePos =event->pos();
        QLineF currentLine = line();
        
        // 根据线条方向限制移动
        if (m_isVertical) {
            // 垂直线：只允许水平移动，保持y坐标不变
            qreal deltaX = scenePos.x() -  m_dragOffset.x();
            setLine(currentLine.x1() + deltaX, currentLine.y1(), 
                   currentLine.x2() + deltaX, currentLine.y2());
        } else {
            // 水平线：只允许垂直移动，保持x坐标不变
            qreal deltaY = scenePos.y() - m_dragOffset.y();
            setLine(currentLine.x1(), currentLine.y1() + deltaY, 
                   currentLine.x2(), currentLine.y2() + deltaY);
        }
        m_dragOffset=scenePos;
        emit positionChanged();
        // 通知场景更新
        scene()->update();
    }
    
    QGraphicsLineItem::mouseMoveEvent(event);
    event->accept();
}

void DraggableLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isDragging = false;
        QGraphicsLineItem::mouseReleaseEvent(event);
        event->accept();
    }
}

void DraggableLineItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    // 根据线条方向设置光标
    if (m_isVertical) {
        setCursor(Qt::SizeHorCursor);
    } else {
        setCursor(Qt::SizeVerCursor);
    }
    QGraphicsLineItem::hoverEnterEvent(event);
}

void DraggableLineItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    setCursor(Qt::ArrowCursor);
    QGraphicsLineItem::hoverLeaveEvent(event);
}

QVariant DraggableLineItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange) {
        // 位置发生变化时发出信号
        emit positionChanged();
    }

    return QGraphicsLineItem::itemChange(change, value);
}
